<%@ page language="java" contentType="text/html; charset=UTF-8" 
import="com.google.appengine.api.datastore.KeyFactory,java.util.*,org.windblog.core.entity.*,org.windblog.core.*"  pageEncoding="UTF-8"%>
<%

	List<File> files = null ;  //要列出的link
	files = FileService.getFiles();
	
	String mnp     = request.getParameter("message");
	int messageNum = 0;
	if(mnp!=null)
		messageNum = Integer.parseInt(mnp);
	String message = null;
	switch(messageNum){
		case 1  : message = "上传文件失败.";break;
		case 2  : message = "上传文件成功.";break;
		case 3  : message = "修改文件失败.";break;
		case 4  : message = "修改文件成功.";break;
		case 5  : message = "删除文件失败.";break;
		case 6  : message = "删除文件成功.";break;
		default : message = null;
	}
	
	
%>
<%!
	static String typeToImage(File file)
	{
		String src = "";
		String contentType = file.getContentType();
		String[] imgTypes = {"image/GIF","image/JPEG","image/pjpeg","image/png"};
		String[] audioTypes = {"audio/mpeg","audio/x-ms-wma"};
		String[] archiveTypes = {"application/octet-stream"};
		String[] videoTypes = {"video/mp4"};
		String[] docTypes = {"application/msword"};
		String[] sheetTypes = {"application/vnd.ms-excel"};
		String[] textTypes = {"text/plain"};
		if(inType(contentType,imgTypes))
		{
			src = file.getDownloadLink();
		}else if(inType(contentType,audioTypes)){
			src = "image/audio.png";
		}else if(inType(contentType,archiveTypes)){
			src = "image/archive.png";
		}else if(inType(contentType,videoTypes)){
			src = "image/video.png";
		}else if(inType(contentType,docTypes)){
			src = "image/document.png";
		}else if(inType(contentType,sheetTypes)){
			src = "image/spreadsheet.png";
		}else if(inType(contentType,textTypes)){
			src = "image/text.png";
		}else{
			src = "image/default.png";
		}
			
		return "<img style=\"max-width:50px;max-height:50px;\" src=\""+src+"\"/>";
	}
	static Boolean inType(String type,String[] types)
	{
		for(String tmp : types)
		{
			if(type.equalsIgnoreCase(tmp))
				return true;
		}
		return false;
	}
%>
		<jsp:include page="header.jsp"></jsp:include>
		<jsp:include page="sidebar.jsp"></jsp:include>
		<script type="text/javascript">
			$(function(){
				// Accordion
				$("#accordion").accordion("activate",1);
				$("#media_library").prepend(">");
			});
		</script>
		<div id="content">
			<h2>Media Library</h2>
			<hr/>
			<% if(message!=null){%>
			<div class="ui-widget" style="width:50em">
			<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"> 
				<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
				<%=message%>
				</p>
			</div>
			</div><br/>
			<%} 
			%>
			<div id="postspanel" class="ui-widget ui-widget-content ui-corner-all" style="margin-top:10px;padding:0;width:800px"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Posts</h3>
				<table id="posts" border="1" cellspacing="0" style="width:100%">
					<thead>
					<tr>
						<th style="width:60px">Type</th>
						<th style="width:200px">File</th>
						<th>Author</th>
						<th>Date</th>
					</tr>
					</thead>
					<%
						
						if(files!=null)
						{
							for(File file:files)
							{
								%>
						
					<tr  class="mzone">
						<td><%=typeToImage(file) %></td>
						<td><strong><a href="<%=file.getDownloadLink() %>"><%= file.getTitle() %> </a></strong><br/><%=file.getContentType() %><div>&nbsp;<span class="Action" style="display:none"><a href="<%=file.getEditLink() %>">Edit </a>| <a href="<%=file.getDeleteLink() %>">Delete</a>| <a href="<%=file.getViewLink() %>">View</a></span></div></td>
						<td><%=file.getAuthor() %></td>
						<td><%=file.getDate() %></td>
					</tr>	
								<%
							}
						}
					%>
				</table>
			</div> 
		</div>
		<jsp:include page="footer.jsp"></jsp:include>
